/********************************************************************

 modinit.c - standard initialisation for Opus 5 modules
             ( ... slightly changed by me ... :-) )

 Typically it is not required for you to change here anything,
 only if you use a different compiler...
 In this case you may refer also to CLib37.lha (AmiNet).

 If your module does require the datatypes library or
 rexxsyslib.library, just define DATATYPES or AREXX in the compiler
 options of the smakefile.

 Function: It does open/close the libraries you may need (add yours,
 if they are not here) and make them available for the whole module.

 ********************************************************************/

#ifndef _DOPUS_MODULE_DEF
#define _DOPUS_MODULE_DEF
#include <dopus/modules.h>
#endif

#ifndef CLIB_EXEC_PROTOS_H
#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>
#endif

#ifndef EXEC_MEMORY_H
#include <exec/memory.h>
#endif

#ifndef CLIB_LOCALE_PROTOS_H
#include <clib/locale_protos.h>
#include <pragmas/locale_pragmas.h>
#endif

/********************************************************************/

// some prototypes for the functions here
// needed by SAS to create the library header
int  __saveds __UserLibInit( void );
void __saveds __UserLibCleanup( void );

struct Library *DOSBase;
struct Library *AslBase;
struct Library *GfxBase;
struct Library *DOpusBase;
struct Library *LocaleBase;
struct Library *UtilityBase;
struct Library *DiskfontBase;
struct Library *GadToolsBase;
struct Library *IntuitionBase;

#ifdef DATATYPES
struct Library *DataTypesBase;
#endif
#ifdef AREXX
struct Library *RexxSysBase; 
#endif

// our module memorypool
APTR mempool;

// since we want that this (and only this) process can not be
// launched multiple times, we need something to check, if it is
// already running. To pass arguments later to this process we
// need the IPCData of this process for addressing. So we use
// simply the IPCData to detect if the process is already running.
// But take care: It is better to protect this variable with a
// semaphore, if there is more "traffic" :)

IPCData *proc_ipc; 

// Locale pointer
struct DOpusLocale *locale;

// a prototype from buildinstrings.c
extern void init_locale_data(struct DOpusLocale *locale);

/********************************************************************/

// Library initialisation code
int __saveds __UserLibInit()
{
        // Initialise pointers
        AslBase = 0;
        GfxBase = 0;
        DOpusBase = 0;
        LocaleBase = 0;
        UtilityBase = 0;
        DiskfontBase = 0;
        GadToolsBase = 0;
        IntuitionBase = 0;
        locale = 0;
        mempool = NULL;
		  proc_ipc = NULL; 
                  
#ifdef DATATYPES                  
        DataTypesBase = 0;
#endif            
#ifdef AREXX
        RexxSysBase = 0;
#endif
                                 
        // Get DOS library (can't really fail)
        DOSBase = OpenLibrary( "dos.library", 0 );

        // Open other libraries we need
        if( !(AslBase = OpenLibrary("asl.library", 37))              ||
            !(GfxBase = OpenLibrary("graphics.library", 37))         ||
            !(DOpusBase = OpenLibrary("dopus5.library", 55))         ||
            !(UtilityBase = OpenLibrary("utility.library", 37))      ||            
            !(GadToolsBase = OpenLibrary( "gadtools.library", 37))   ||
            !(IntuitionBase = OpenLibrary( "intuition.library", 37)) ||
                                
#ifdef DATATYPES                                
            !(DataTypesBase = OpenLibrary( "datatypes.library", 39)) ||
#endif                          
#ifdef AREXX
            !(RexxSysBase = OpenLibrary("rexxsyslib.library", 0))    ||
#endif      
            !(DiskfontBase = OpenLibrary("diskfont.library", 37)) )
             return 1;
        
        // Creating our memorypool and use it immediate for the locale
        if( !(mempool = NewMemHandle(4096, 3072, MEMF_CLEAR|MEMF_PUBLIC)) ||
            !(locale = AllocMemH(mempool, sizeof(struct DOpusLocale))) )
             return 1;

        init_locale_data(locale);

        // Open locale library
        if( LocaleBase = OpenLibrary("locale.library", 38) )
          {
             // Store library pointer
             locale->li_LocaleBase = LocaleBase;

             // Open catalog if name supplied
             if( module_info.locale_name )
               {
                  struct TagItem tags[2];

                  // If MODULEF_CATALOG_VERSION is set, we do version checking
                  tags[0].ti_Tag = (module_info.flags & MODULEF_CATALOG_VERSION) ? OC_Version : TAG_IGNORE;
                  tags[0].ti_Data = module_info.ver;
                  tags[1].ti_Tag = TAG_DONE;

                  // Open catalog
                  locale->li_Catalog = OpenCatalogA( NULL, module_info.locale_name, tags );
               }

             // Get default lolale
             locale->li_Locale = OpenLocale( 0 );
          }
        
        return NULL; // Succeeded
}


// Clean up
void __saveds __UserLibCleanup()
{
     if( mempool )
       {
          if( locale )
            {
               if( LocaleBase )
                 {
                    CloseLocale( locale->li_Locale );
                    CloseCatalog( locale->li_Catalog );
                    CloseLibrary( LocaleBase );
                 }

               FreeMemH(locale);
            }

          FreeMemHandle( mempool );
       }
                  
     // Close libraries
     CloseLibrary( AslBase );
     CloseLibrary( GfxBase );
     CloseLibrary( DOpusBase );
     CloseLibrary( DiskfontBase );
     CloseLibrary( UtilityBase );
                  
#ifdef DATATYPES
     CloseLibrary( DataTypesBase );
#endif            
#ifdef AREXX              
     CloseLibrary( RexxSysBase );
#endif        

     CloseLibrary( GadToolsBase );
     CloseLibrary( IntuitionBase );
     CloseLibrary( DOSBase );
}

/********************************************************************/

// This routine is called by DOpus to find out what the module does
// Do not modify it or move it to an other place !!

ModuleInfo *__asm __saveds L_Module_Identify( register __d0 int num )
{
     // Return module information
     if( num == -1 )
          return &module_info;

     // Valid function number?
     if( num > module_info.function_count ||
         !(module_info.function[num].desc) )
          return 0;

     // Return function description
     return (ModuleInfo *) DOpusGetString( locale, module_info.function[num].desc );
}

/********************************************************************/